package tw.asap.plus.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import tw.asap.plus.R;
import tw.asap.plus.entity.response.ShopInfo;

/**
 * Created by Yax on 15/11/7.
 */
public class MerchantInfoFragment extends Fragment {

    @InjectView(R.id.work_time)
    TextView workTime;
    @InjectView(R.id.store_address)
    TextView storeAddress;
    @InjectView(R.id.tel)
    TextView tel;
    @InjectView(R.id.delivery_scope)
    TextView deliveryScope;

    private ShopInfo shopInfo;
    private View view;
    private static final String ARG_SHOP_INFO = "arg_shop_info";


    public static Fragment newInstance(ShopInfo shopInfo) {
        Fragment fragment = new MerchantInfoFragment();
        Bundle args = new Bundle();
        args.putParcelable(ARG_SHOP_INFO, shopInfo);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            shopInfo = getArguments().getParcelable(ARG_SHOP_INFO);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_merchant_info, null);
        } else {
            if (view.getParent() != null)
                ((ViewGroup) view.getParent()).removeView(view);
        }
        ButterKnife.inject(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (shopInfo != null) {
            workTime.setText(shopInfo.getOpening().concat(" - ").concat(shopInfo.getClosing()));
            storeAddress.setText(shopInfo.getAddress());
            tel.setText(shopInfo.getContact());
            deliveryScope.setText(shopInfo.getShipping_area());
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
